package org.example.medical.controller;


import org.example.medical.entity.Patient;
import org.example.medical.service.PatientService;
import org.example.medical.utils.ApiResult;
import org.example.medical.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/patients")
public class PatientApiController {
    @Autowired
    PatientService patientService;

    // 1. 分页查询接口
    @GetMapping
    public ResponseEntity<ApiResult<PageResult<Patient>>> listPatients(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String gender
    ) {
        PageResult<Patient> result = patientService.getPatients(page, size, name, gender);
        return ResponseEntity.ok(ApiResult.success(result));
    }

    // 2. 更新患者信息
    @PutMapping("/{id}")
    public ResponseEntity<ApiResult<?>> updatePatient(
            @PathVariable Integer id,
            @RequestBody Patient patient
    ) {
        patient.setPatientId(id);
        patientService.updatePatient(patient);
        return ResponseEntity.ok(ApiResult.success(null, "Patient updated successfully"));
    }

    // 3. 软删除患者
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResult<?>> deletePatient(@PathVariable Integer id) {
        patientService.deletePatient(id);
        return ResponseEntity.ok(ApiResult.success(null, "Patient deleted successfully"));
    }

    // 4. 新增患者信息
    @PostMapping
    public ResponseEntity<ApiResult<?>> addPatient(@RequestBody Patient patient) {
        patient.setIsDeleted(0); // 设置逻辑删除标志为未删除
        patientService.addPatient(patient);
        return ResponseEntity.ok(ApiResult.success(null, "Patient added successfully"));
    }

    // 5. 批量软删除患者信息
    @PostMapping("/batchDelete")
    public ResponseEntity<ApiResult<?>> batchDeletePatients(@RequestBody List<Integer> patientIds) {
        patientService.batchDeletePatients(patientIds);
        return ResponseEntity.ok(ApiResult.success(null, "Patients deleted successfully"));
    }
}